package br.com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.connection.ConnectionFactory;
import br.com.controller.TurmaController;

@WebServlet("/CadTurmaServlet")
public class CadTurmaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public CadTurmaServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			doInsert(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	protected void doInsert(HttpServletRequest request, HttpServletResponse response) throws Exception {


		PrintWriter out = response.getWriter();

		request.setCharacterEncoding("UTF-8");        
		response.setCharacterEncoding("UTF-8");
		int ensino = Integer.parseInt(request.getParameter("ensino"));
		int serie = Integer.parseInt(request.getParameter("serie"));
		int turno = Integer.parseInt(request.getParameter("turno"));
		String turma = request.getParameter("nometurma");
		int dletivo = Integer.parseInt(request.getParameter("diasletivos"));
		int ano = Integer.parseInt(request.getParameter("ano"));

		Connection conn = null;

		try {

			conn = new ConnectionFactory().getConnection();
			TurmaController.inserir(conn, ensino, serie, turno, turma, dletivo, ano);
			out.print("<script type=\"text/javascript\">alert(\"Cadastrado com sucesso.\");</script>");
			out.print("<meta http-equiv='refresh'>");
			conn.commit();

		} catch (Exception e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (Exception ex) {ex.printStackTrace();}
		} finally {
			try {
				conn.close();
			} catch (Exception ex) {}
		}
	}
}
